package com.eliansun.util;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;

/**
 * IP地址工具类
 */
public final class IpUtil {
	private static final Logger logger = Logger.getLogger(IpUtil.class);


	/**
	 * 获取访问的ip
	 */
	public static String getIpAddr(HttpServletRequest request) {
		if (request == null) {
			return "127.0.0.1";
		}
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("ProxyUtil-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-ProxyUtil-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
